package cn.jjxx.core.filter.xss;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean; 
import org.apache.shiro.web.filter.mgt.FilterChainManager;
import org.apache.shiro.web.filter.mgt.FilterChainResolver;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.mgt.WebSecurityManager;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanInitializationException;
 


/**
 * 重构shirofileter过滤
 *@Title MyShiroFilterFactoryBean.java
 *@description TODO
 *@time 2020年6月17日 下午1:24:09
 *@author YAOXIONG   ShiroFilterFactoryBean
 *@version 1.0
 *
 */
public class MyShiroFilterFactoryBean extends ShiroFilterFactoryBean {

	private static final Logger log = LoggerFactory.getLogger(MyShiroFilterFactoryBean.class);
	
	
    @Override
    public Class getObjectType() {
        return MySpringShiroFilter.class;
    } 
    
    
    @Override
    public SecurityManager getSecurityManager() {
        return super.getSecurityManager();
    }
    
    
    @Override
    protected AbstractShiroFilter createInstance() throws Exception { 
    	log.debug("----------------身份验证------------------------");
    	log.debug("Creating Shiro Filter instance.");
        SecurityManager securityManager = getSecurityManager();
        if (securityManager == null) {
            String msg = "SecurityManager property must be set.";
            throw new BeanInitializationException(msg);
        } 
 
        if (!(securityManager instanceof WebSecurityManager)) {
            String msg = "The security manager does not implement the WebSecurityManager interface.";
            throw new BeanInitializationException(msg);
        }
        FilterChainManager manager = createFilterChainManager();
 
        PathMatchingFilterChainResolver chainResolver = new PathMatchingFilterChainResolver();
        chainResolver.setFilterChainManager(manager);
 
        return new MySpringShiroFilter((WebSecurityManager) securityManager, chainResolver); 
        
    }
    
    private static final class MySpringShiroFilter extends AbstractShiroFilter {

        protected MySpringShiroFilter(WebSecurityManager webSecurityManager, FilterChainResolver resolver) {
            super();
            if (webSecurityManager == null) {
                throw new IllegalArgumentException("WebSecurityManager property cannot be null.");
            }
            setSecurityManager(webSecurityManager);
            if (resolver != null) {
                setFilterChainResolver(resolver);
            }
        }
        
        @Override
        protected ServletResponse wrapServletResponse(HttpServletResponse orig, ShiroHttpServletRequest request) {
            return new MyShiroHttpServletResponse(orig, getServletContext(), request);
        }
    } 
}
